#!/bin/bash

Echo()
{
	case $1 in
	    success)  flag="\033[1;32m"
	    ;;
	    failure)  flag="\033[1;31m"
	    ;;
	    warning)  flag="\033[1;33m"
	    ;;
	    msg)  flag="\033[1;34m"
	    ;;
	    *)  flag="\033[1;34m"
	    ;;
	esac
	if [[ $LANG =~ [Uu][Tt][Ff] ]]
	then
		echo -e "${flag}${2}\033[0m"
	else
		echo -e "${flag}${2}\033[0m" | iconv -f utf-8 -t gbk
	fi
}

if [ ! $1 ];then
	Echo "msg" "缺少必要参数"
	Echo "msg" "创建快照请使用Create，如sh $(basename $0) Create"
	Echo "msg" "恢复快照请使用Restore，如sh $(basename $0) Restore"
	Echo "msg" "删除快照请使用Delete，如sh $(basename $0) Delete"
	exit
else
	flag=$1
fi

#创建快照
Create_snapshot()
{
	virsh list --all
	read -p "please input ebs_name:" ebs_name
	read -p "please input snapsho_name:" snapsho_name
	virsh snapshot-create-as $ebs_name $snapsho_name
	virsh snapshot-current $ebs_name|grep name
	virsh snapshot-list $ebs_name
	qemu-img info /vmdata/${ebs_name}/${ebs_name}_os.qcow2
	qemu-img info /vmdata/${ebs_name}/${ebs_name}_data.qcow2
}

#恢复快照
Restore_snapshot()
{
	virsh list --all
	read -p "please input ebs_name:" ebs_name
	virsh snapshot-list $ebs_name
	read -p "please input snapsho_name:" snapsho_name
	virsh snapshot-revert $ebs_name $snapsho_name
	virsh snapshot-current $ebs_name|grep name
}

#删除快照
Delete_snapshot()
{
	virsh list --all
	read -p "please input ebs_name:" ebs_name
	virsh snapshot-list $ebs_name
	read -p "please input snapsho_name:" snapsho_name
	virsh snapshot-delete $ebs_name $snapsho_name
	virsh snapshot-list $ebs_name

}
Main()
{
	[ "$flag" == "Create" ] && Create_snapshot
	[ "$flag" == "Restore" ] && Restore_snapshot
	[ "$flag" == "Delete" ] && Delete_snapshot
}
